package com.jieao.backend.controller.content;

import java.util.List;

import com.jieao.content.domain.Album;
import com.jieao.content.dto.AlbumDto;
import com.jieao.content.dto.ArticleDto;
import com.jieao.content.service.IAlbumService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.jieao.common.annotation.Log;
import com.jieao.common.enums.BusinessType;
import com.jieao.common.core.controller.BaseController;
import com.jieao.common.core.domain.AjaxResult;
import com.jieao.common.utils.poi.ExcelUtil;
import com.jieao.common.core.page.TableDataInfo;

/**
 * 图片Controller
 *
 * @author jieao
 * @date 2020-09-23
 */
@Controller
@RequestMapping("/content/album")
public class AlbumController extends BaseController {
    private String prefix = "backend/content/album";

    @Autowired
    private IAlbumService albumService;

    @RequiresPermissions("content:album:view")
    @GetMapping()
    public String album() {
        return prefix + "/album";
    }

    /**
     * 查询图片列表
     */
    @RequiresPermissions("content:album:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Album album) {
        startPage();
        List<AlbumDto> list = albumService.selectAlbumList(album);
        return getDataTable(list);
    }

    /**
     * 导出图片列表
     */
    @RequiresPermissions("content:album:export")
    @Log(title = "图片", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Album album) {
        List<AlbumDto> list = albumService.selectAlbumList(album);
        ExcelUtil<AlbumDto> util = new ExcelUtil<AlbumDto>(AlbumDto.class);
        return util.exportExcel(list, "album");
    }

    /**
     * 新增图片
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存图片
     */
    @RequiresPermissions("content:album:add")
    @Log(title = "图片", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Album album) {
        return toAjax(albumService.insertAlbum(album));
    }

    /**
     * 修改图片
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        AlbumDto album = albumService.selectAlbumById(id);
        mmap.put("album", album);
        return prefix + "/edit";
    }

    /**
     * 修改保存图片
     */
    @RequiresPermissions("content:album:edit")
    @Log(title = "图片", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Album album) {
        return toAjax(albumService.updateAlbum(album));
    }

    /**
     * 删除图片
     */
    @RequiresPermissions("content:album:remove")
    @Log(title = "图片", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(albumService.deleteAlbumByIds(ids));
    }

}
